package mao.exaimnation.tengxun;

import java.util.*;

/**
 * @author Totoro
 * @date 2020/9/6
 * @explain: 统计字符串出现的次数，并按照字典序输出（同样的出现次数的情况下）
 *          思路：使用TreeMap实现key的排序，使用Arrays.parallelSort实现稳定的排序。
 *          注意：不同通过Collection.sort()。因为只集合，没有稳定的概念。
 */

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        while (scanner.hasNext()){
            int n = scanner.nextInt();
            int k = scanner.nextInt();
            TreeMap<String,Integer> map = new TreeMap<>();
            for (int i = 0; i < n; i++) {
                String next = scanner.next();
                if (map.containsKey(next)){
                    map.put(next,map.get(next)+1);
                }else {
                    map.put(next,1);
                }
            }

            Map.Entry<String,Integer>[] mapEntry =  new Map.Entry[map.size()];
            Set<Map.Entry<String, Integer>> entries = map.entrySet();
            Iterator<Map.Entry<String, Integer>> iterator = entries.iterator();
            for (int i = 0; i < entries.size(); i++) {
                mapEntry[i] = iterator.next();
            }
            Arrays.parallelSort(mapEntry,(entry1,entry2)->{
                return entry2.getValue()-entry1.getValue();
            });
            for (int i = 0; i < k; i++) {
                System.out.println(mapEntry[i].getKey());
            }

            Arrays.parallelSort(mapEntry,(entry1,entry2)->{
                return entry1.getValue()-entry2.getValue();
            });
            for (int i = 0; i < k; i++) {
                System.out.println(mapEntry[i].getKey());
            }
        }
    }
}
